package com.yeyks.controller.other;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.base.PageResult;
import com.yeyks.other.dal.domain.Carousel;
import com.yeyks.other.dto.CarouselAddParam;
import com.yeyks.other.dto.CarouselListParam;
import com.yeyks.other.dto.CarouselUpdateParam;
import com.yeyks.other.service.CarouselService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author fuchangshuai
 * @since 2019-07-03
 */
@Api(tags = "轮播图模块")
@RestController
@RequestMapping(value = "/carousel")
public class CarouselController {

    @Autowired
    private CarouselService carouselService;

    @ApiOperation(value = "v4.0.0 --> 列表", notes = "参数都可不填", response = Carousel.class)
    @PostMapping(value = "/list")
    public AjaxResponseBody<PageResult<Carousel>> list(@Validated CarouselListParam param) {
        PageResult<Carousel> carousels = carouselService.myList(param);
        return AjaxResponseBody.success(carousels);
    }

    @ApiOperation(value = "v4.0.0 --> 新增")
    @PostMapping(value = "/add")
    public AjaxResponseBody<Boolean> add(@Validated CarouselAddParam param) {
        List<Carousel> carousels = carouselService.myList(param.getStatus(), param.getType());
        Arguments.isTrue(null != carousels && 5 == carousels.size(), "轮播图超限");
        return AjaxResponseBody.success(carouselService.save(BeanUtil.toBean(param, Carousel.class)));
    }

    @ApiOperation(value = "v4.0.0(4.2修改) --> 编辑")
    @PostMapping(value = "/update")
    public AjaxResponseBody<Boolean> update(@Validated CarouselUpdateParam param) {
        Carousel byId = carouselService.getById(param.getId());
        Arguments.beanIsNull(byId, "该轮播图不存在");
        if (StrUtil.isNotBlank(param.getImage())) {
            byId.setImage(param.getImage()).setUpdateTime(new Date());
        }
        if (StrUtil.isNotBlank(param.getUrl())) {
            byId.setUrl(param.getUrl());
        }
        return AjaxResponseBody.success(carouselService.updateById(byId));
    }

    @ApiOperation(value = "v4.2 --> 删除轮播图链接", notes = "可以不使用，直接使用编辑接口")
    @PostMapping(value = "/del/url")
    public AjaxResponseBody<Boolean> delUrl(Integer id) {
        Arguments.intIsNull(id, "轮播图ID不能为空");
        Carousel byId = carouselService.getById(id);
        Arguments.beanIsNull(byId, "该轮播图不存在");
        Arguments.strIsNull(byId.getUrl(), "该轮播图无链接");
        byId.setUrl(null).setUpdateTime(new Date());
        return AjaxResponseBody.success(carouselService.updateById(byId));
    }

    @ApiOperation(value = "v4.0.0 --> 删除(设置无效)")
    @PostMapping(value = "/del")
    public AjaxResponseBody<Boolean> delete(Integer id) {
        Arguments.intIsNull(id, "轮播图id不能为空");
        Carousel byId = carouselService.getById(id);
        Arguments.beanIsNull(byId, "该轮播图不存在");
        List<Carousel> carousels = carouselService.myList(0, 1);
        Arguments.isTrue(null != carousels && 2 == carousels.size(), "图片少于两张不能删除");
        byId.setStatus(1).setUpdateTime(new Date());
        return AjaxResponseBody.success(carouselService.updateById(byId));
    }


}